package com.kingint.web.sys.service.impl;

import com.kingint.web.sys.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis操作Service的实现类
 * https://gitee.com/zscat-platform/mall on 2018/8/7.
 */
@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public boolean expire(String key, long expire) {
        return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    @Override
    public void remove(String key) {
        stringRedisTemplate.delete(key);
    }

    @Override
    public Long increment(String key, long delta) {
        return stringRedisTemplate.opsForValue().increment(key, delta);
    }
    @Override
    public void zAdd(String key, Object value, double scoure) {
        ZSetOperations<String, String> zSet = stringRedisTemplate.opsForZSet();
        zSet.add(key, String.valueOf(value),scoure);
    }

    @Override
    public double score(String key, Object key2) {
        ZSetOperations<String, String> zSet = stringRedisTemplate.opsForZSet();
        return zSet.score(key,key2);
    }

    @Override
    public Set<String> reverseRange(String key, long start, long end) {
        ZSetOperations<String, String> zSet = stringRedisTemplate.opsForZSet();
        return zSet.reverseRange(key, start, end);
    }

    @Override
    public Long rank(String key, Object key2) {
        ZSetOperations<String, String> zSet = stringRedisTemplate.opsForZSet();
        return zSet.rank(key, key2);
    }

    @Override
    public Long lpush(String key, String value) {
        return stringRedisTemplate.opsForList().leftPush(key,value);
    }

    @Override
    public String lpop(String key) {
        return stringRedisTemplate.opsForList().leftPop(key);
    }

    @Override
    public Long rpush(String key, String value) {
        return stringRedisTemplate.opsForList().rightPush(key,value);
    }

    @Override
    public String rpop(String key) {
        return stringRedisTemplate.opsForList().rightPop(key);
    }

    @Override
    public Long llen(String key) {
        return stringRedisTemplate.opsForList().size(key);
    }

    @Override
    public List<String> lrang(String key, Integer start,Integer end) {
        return stringRedisTemplate.opsForList().range(key,start,end);
    }

    @Override
    public String lindex(String key1, long index) {
        return stringRedisTemplate.opsForList().index(key1, index);
    }

    @Override
    public void hset(String key1, String key2, String value) {
        stringRedisTemplate.opsForHash().put(key1,key2,value);
    }

    @Override
    public String hget(String key1, String key2) {
        return (String) stringRedisTemplate.opsForHash().get(key1, key2);
    }

    @Override
    public List hvalues(String key) {
        return stringRedisTemplate.opsForHash().values(key);
    }

    @Override
    public Map hentries(String key) {
        return stringRedisTemplate.opsForHash().entries(key);
    }

    @Override
    public Long hexists(String key) {
        return stringRedisTemplate.opsForHash().size(key);
    }

    @Override
    public Long hremove(String key,Object...key2) {
        return stringRedisTemplate.opsForHash().delete(key,key2);
    }
}
